package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.domain.Student;

import java.util.ArrayList;

public class OtherStudentDao implements BaseStudentDao {
    // 1. 创建学生对象集合容器
    private static ArrayList<Student> students = new ArrayList<>();

    static {
        Student stu1 = new Student("heima001", "张三", "23", "1999-11-11");
        Student stu2 = new Student("heima002", "李四", "24", "2000-11-11");

        students.add(stu1);
        students.add(stu2);
    }

    @Override
    public boolean addStudent(Student stu) {
        students.add(stu);
        return true;
    }

    @Override
    public Student[] findAllStudent() {
        Student[] stus = new Student[students.size()];
        for (int i = 0; i < stus.length; i++) {
            stus[i] = students.get(i);
        }
        return stus;
    }

    @Override
    public void deleteStudentById(String delId) {
        // 1. 查找 id 在容器中所在的位置
        int index = getIndex(delId);
        students.remove(index);
    }

    @Override
    public int getIndex(String id) {
        int index = -1;
        for (int i = 0; i < students.size(); i++) {
            Student stu = students.get(i);
            if (stu != null && stu.getId().equals(id)) {
                index = i;
                break;
            }
        }
        return index;
    }

    @Override
    public void updateStudent(Student stu) {
        if (stu == null) {
            return;
        }
        int index = getIndex(stu.getId());
        students.set(index, stu);
    }

    @Override
    public void updateStudent(String updateId, Student stu) {
        if (stu == null) {
            return;
        }
        int index = getIndex(updateId);
        students.set(index, stu);
    }
}
